/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pckFiles;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
    // Ligne(0,1,2,3) | Col(A,B,C) | Valeur     tous en nombre


/**
 *
 * @author Portable-Maison
 */
public class HoraireExcel {

    // Modèle
    Workbook gaModel;
    InputStream gaHoraireRef;

    //Fichier étudiant
    WritableWorkbook gaHoraire;
    WritableSheet gaFeuil;
    File gaFichierHoraire;

    // Information étudiant
    String gsEtudiant;
    int giNombreCours = 0;

    public HoraireExcel()
    {
    }

    public HoraireExcel(String psEtudiant) throws Exception
    {
    // Initiliation de étudiant
    gsEtudiant = psEtudiant;
    // Chargement du fichier "modèle"
    gaHoraireRef = new FileInputStream("horaire.xls");
    gaModel = Workbook.getWorkbook(gaHoraireRef);
    // Création du fichier de sortie
    gaFichierHoraire = new File(psEtudiant+".xls");
    gaHoraire = Workbook.createWorkbook(gaFichierHoraire, gaModel);
    // Récupération de l'onglet courant (le premier onglet)
    gaFeuil = gaHoraire.getSheet(0);
    gaFeuil.setName (psEtudiant);
    // Ajouter le nom de ETUDIANT au titre
    EcrireNomEtudiant();
    EcrireEntetePrincipale("DÉTAILS");
    EcrireEnteteSecondaire("Information Dîner",6,5);
    EcrireCoursHoraire("VIE ÉTUDIANTE","","",2,24,Colour.PERIWINKLE,"");
    EcrireCoursHoraire("VIE ÉTUDIANTE","","",2,27,Colour.PERIWINKLE,"");
    EcrireCoursHoraire("VIE ÉTUDIANTE","","",2,30,Colour.PERIWINKLE,"");
    AjouterCours();
    // Gestion des caractères accentue
    EcrireHoraire();
    // Fermeture Horaire
    gaHoraireRef.close ();
    }

    private void EcrireHoraire() throws WriteException
    {
    WritableFont laHeure = new WritableFont(WritableFont.createFont ("Calibri"), 10, WritableFont.BOLD);
    laHeure.setColour (Colour.BLACK);
    WritableCellFormat laStyle= new WritableCellFormat(laHeure);
    laStyle.setBackground (Colour.GRAY_25);
    laStyle.setAlignment (Alignment.CENTRE);

    ecrireTexte("À",0,4,laStyle);
    ecrireTexte("À",0,7,laStyle);
    ecrireTexte("À",0,10,laStyle);
    ecrireTexte("À",0,13,laStyle);
    ecrireTexte("À",0,16,laStyle);
    ecrireTexte("À",0,19,laStyle);
    ecrireTexte("À",0,22,laStyle);
    ecrireTexte("À",0,25,laStyle);
    ecrireTexte("À",0,28,laStyle);
    ecrireTexte("À",0,31,laStyle);
    }

    private void EcrireEntetePrincipale(String psTexte) throws WriteException
    {
    WritableFont laEntete = new WritableFont(WritableFont.createFont ("Calibri"), 12, WritableFont.BOLD);
    laEntete.setColour(Colour.BLACK);
    laEntete.setUnderlineStyle (UnderlineStyle.SINGLE);
    WritableCellFormat laStyle= new WritableCellFormat(laEntete);
    laStyle.setBackground (Colour.GRAY_25);
    laStyle.setAlignment (Alignment.CENTRE);
    laStyle.setVerticalAlignment (VerticalAlignment.CENTRE);
    laStyle.setBorder (Border.ALL, BorderLineStyle.MEDIUM);

    ecrireTexte(psTexte,6,1,laStyle);
    }

    private void EcrireEnteteSecondaire(String psTexte, 
                                        int piCol,
                                        int piRow) throws WriteException
    {
    WritableFont laEntete = new WritableFont(WritableFont.createFont ("Calibri"), 11, WritableFont.BOLD);
    laEntete.setColour(Colour.BLACK);
    laEntete.setUnderlineStyle (UnderlineStyle.SINGLE);
    WritableCellFormat laStyle= new WritableCellFormat(laEntete);
    laStyle.setAlignment (Alignment.CENTRE);
    laStyle.setVerticalAlignment (VerticalAlignment.CENTRE);
    laStyle.setBorder (Border.TOP, BorderLineStyle.THIN);
    laStyle.setBorder (Border.BOTTOM, BorderLineStyle.THIN);
    laStyle.setBorder (Border.RIGHT, BorderLineStyle.MEDIUM);
    
    ecrireTexte(psTexte,piCol,piRow,laStyle);
    }

    public final void EcrireNomEtudiant() throws WriteException
    {
    WritableFont laEtd = new WritableFont(WritableFont.createFont ("Calibri"), 11, WritableFont.BOLD);
    laEtd.setColour(Colour.RED);
    WritableCellFormat laTitle = new WritableCellFormat(laEtd);
    laTitle.setAlignment (Alignment.CENTRE);
    
    ecrireTexte(gsEtudiant,0,0,laTitle);
    }


    public final void EcrireReferenceCours(String psCours,
                                           String psEnseignant,
                                           Colour paColor) throws WriteException
    {
    WritableFont laCours = new WritableFont(WritableFont.createFont ("Calibri"), 10, WritableFont.NO_BOLD);
    laCours.setColour(Colour.BLACK);
    // Le style base
    WritableCellFormat laStyle = new WritableCellFormat(laCours);
//    laStyle.setAlignment (Alignment.LEFT);
//    laStyle.setVerticalAlignment (VerticalAlignment.CENTRE);
    laStyle.setBackground (paColor);
    // Insertion
    if (!psEnseignant.equals (""))
    {
    ecrireTexte(psCours + " - " + psEnseignant,6,22+giNombreCours,laStyle);
    }
    else
    {
    ecrireTexte(psCours,6,22+giNombreCours,laStyle);
    }

    }

    public final void EcrireCoursHoraire(String psCours,
                                         String psLocal,
                                         String psGroupe,
                                         int piCol,
                                         int piRow,
                                         Colour paColor,
                                         String psProf) throws WriteException
    {
    WritableFont laCours = new WritableFont(WritableFont.createFont ("Calibri"), 11, WritableFont.BOLD);
    laCours.setColour(Colour.BLACK);
    // Le style base
    WritableCellFormat laStyle = new WritableCellFormat(laCours);
    laStyle.setAlignment (Alignment.CENTRE);
    laStyle.setVerticalAlignment (VerticalAlignment.CENTRE);
    laStyle.setBackground (paColor);
    // Le style fond
    WritableCellFormat laStyle2 = new WritableCellFormat(laCours);
    laStyle2.setAlignment (Alignment.CENTRE);
    laStyle2.setVerticalAlignment (VerticalAlignment.CENTRE);
    laStyle2.setBackground (paColor);
    laStyle2.setBorder (Border.BOTTOM, BorderLineStyle.MEDIUM);

    int liI = 1;
    int liStart = 0;
    int liOcc = 0;
    String[] psLigne = new String[2];
    while(liI < psCours.length ())
    {
    if (liI%35 == 0)
        {
        psLigne[liOcc] = psCours.substring (liStart,liI);
        liStart = liI;
        liOcc++;
        }
    liI++;
    }
    if (psLigne[1] == null)
    {
        ecrireTexte("",piCol,piRow,laStyle);
        ecrireTexte(psCours,piCol,piRow+1,laStyle);
        ecrireTexte("",piCol,piRow+2,laStyle2);
    }
    else
    {
        ecrireTexte(psLigne[0],piCol,piRow,laStyle);
        ecrireTexte(psLigne[1],piCol,piRow+1,laStyle);
        ecrireTexte("Gr. " + psGroupe + "("+psLocal+")",piCol,piRow+2,laStyle2);
    }
    EcrireReferenceCours(psCours,psProf,paColor);
    // Ecrire cours dans information
    }

    public void AjouterCours()
    {
        giNombreCours++;
    }

    public final void ecrireTexte(String psTexte, int piCol, int piRow, WritableCellFormat paStyle) throws WriteException
    {
    Label laMessage = new Label(piCol,piRow,psTexte,paStyle);
    gaFeuil.addCell (laMessage);
    }

    @Override
    public void finalize()
    {
        try {
            gaHoraire.write ();
            gaHoraire.close ();
        }
        catch (WriteException ex) {
            Logger.getLogger (HoraireExcel.class.getName()).log (Level.SEVERE, null, ex);
        }
        catch (IOException ex) {
            Logger.getLogger (HoraireExcel.class.getName()).log (Level.SEVERE, null, ex);
        }
    }
}


